<?php

namespace app\index\controller;

use app\common\service\WxPayService;
use library\Controller;
use think\Db;
use think\facade\Config;

/**
 * 我的订单
 * Class Order
 * @package app\index\controller
 */
class Order extends Base
{
    /**
     * 订单列表
     */
    public function index()
    {
    	$where[] = ['o.company_id','eq',$this->company['id']];
		$where[] = ['o.is_deleted','eq',0];
        $status = $this->request->get('status');
        if($status){
        	$where[] = ['o.status','=',$status];
        }
        $search =  $this->request->get('search');
		if($search){
			$where[] = ['o.code','like','%'.$search.'%'];
		}
		$list = Db::name('system_package_order')
				->alias('o')
				->join('system_package p','o.package_id = p.id')
				->where($where)
				->field('o.id,o.code,o.package_id,o.create_at,o.status,o.receivable,o.type,o.setmeal_type,p.title')
    			->order('o.id desc')
    			->paginate(10,false,['query'=>request()->param(),'type' => 'page\Page','var_page'  => 'page']);
		$this ->list = $list; 
		//待付款
        $this ->to_pay = Db::name('system_package_order')->where(['company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>1])->count(); 
    	$this->fetch();
    }

    
    
    /**
     * 付款
     */
    public function payment()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		if(empty($data['id']) || !$data['id']){
				$this->error('参数错误');
			}
    		$order = Db::name('system_package_order')->where(['id'=>$data['id'],'company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>[1,2]])->find();
    		if(!$order){
				$this->error('订单信息有误，无法付款');
			}
			Db::name('system_package_order')->where(['id'=>$data['id']])->update(['status'=>2]);
			//更新财务账单
        	Db::name('system_financial_bill')->where(['bill_type'=>2,'jump_id'=>$data['id']])->update(['status'=>2]);
        	$this->success('付款中......', url('index/order/paymentSuccess'));
    	}else{
    		$id = $this->request->param('id');
			$order = Db::name('system_package_order')->where(['id'=>$id,'company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>[1,2]])->find();
			if(!$order){
				$this->redirect('index/publicot/transition', ['info'=>'暂无订单信息','type'=>'8']);
			}
			$order['package_name'] = Db::name('system_package')->where(['id'=>$order['package_id']])->value('title');
			$this ->info = $order;
    		$this->fetch();
    	}   	
    }
    
    
    /**
     * 生成微信付款码
     */
    public function payment_code()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		if(empty($data['id']) || !$data['id']){
				$this->error('参数错误');
			}
			$order = Db::name('system_package_order')->where(['id'=>$data['id'],'company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>[1,2]])->find();
    		if(!$order){
				$this->error('订单信息有误，无法付款');
			}
			$wxpay = new WxPayService();
			$body = '服务费';
			$pay_code = $order['code'].'_native';
			$platform = Config::get()['wechat']['platform'];
			$result = $wxpay->pay($pay_code, $order['receivable'], 2,$body,$platform);
			if(!$result){
				$this->error('支付二维码获取失败，请稍后再试');
			}
			if(!empty($result['code']) && $result['code'] == 0){
				$this->error('支付二维码获取失败，原因：'.$result['data']);
			}
			$this->success('获取成功',['pay_code'=>$result]);
    	}
    }
    
	
	/**
     * 查询付款信息
     */
    public function query_order()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		if(empty($data['id']) || !$data['id']){
				$this->error('参数错误');
			}
			$order = Db::name('system_package_order')->where(['id'=>$data['id'],'company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>[3,4],'payment_method'=>3])->find();
    		if(!$order){
				$this->error('账单未付款');
			}else{
				$this->success('付款成功');
			}
    	}
    }
    
	
	
	/**
     * 付款成功
     */
    public function paymentSuccess()
    {
        $this->fetch();
    }
    
    
    /**
     * 取消
     */
    public function cancel()
    {
    	$data = $this->request->post();
		if(empty($data['id']) || !$data['id']){
			$this->error('参数错误');
		}
		$order = Db::name('system_package_order')->where(['id'=>$data['id'],'company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>[1,2]])->find();
		if(!$order){
			$this->error('订单信息有误，无法取消');
		}
		Db::name('system_package_order')->where(['id'=>$data['id']])->update(['status'=>5]);
		//更新财务账单
    	Db::name('system_financial_bill')->where(['bill_type'=>2,'jump_id'=>$data['id']])->update(['status'=>4]);
    	$this->success('取消成功');
    }
	
}
